package com.suneee.base.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.suneee.base.filter.UserFilter;
import com.suneee.component.cache.thread.ThreadLocalCache;

public class ImageServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -698805049345241679L;

	 
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		HttpServletRequest request = (HttpServletRequest)ThreadLocalCache.getObject(UserFilter.REQUEST);
		HttpServletResponse response = (HttpServletResponse)ThreadLocalCache.getObject(UserFilter.RESPONSE);
		 
		response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  
        response.setContentType("image/jpeg");  
          
        //生成随机字串  
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);  
        
        //存入会话session  
        HttpSession session = request.getSession();  
        session.setAttribute("rand", verifyCode.toLowerCase());  
        
        String s = (String)session.getAttribute("rand");
        System.out.println(s);
        
        //生成图片  
        int w = 200, h = 80;  
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
	}


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}
	
}
